package code.sbxj.qwd.com.sbxj;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.blankj.utilcode.util.CacheDiskUtils;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;


@ContentView(R.layout.activity_checkline)
public class CheckPointActivity extends BaseActivity {

    @ViewInject(R.id.listview)
    private ListView listview;


    MyAdapter myadapter;
    JSONArray dataArray = new JSONArray();
    boolean isoffline;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        setTitleText("巡检线：" + getIntent().getExtras().getString("linename"));
        initView();
//        getUserInfo();
        getTasklist(getIntent().getExtras().getInt("lineid"));
       }

    void initView() {
        myadapter = new MyAdapter();
        listview.setAdapter(myadapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                Intent intent=new Intent();
                intent.setClass(CheckPointActivity.this,DeviceListActivity.class);
                try {
                    intent.putExtra("devicename",dataArray.getJSONObject(i).optString("name"));
                    intent.putExtra("linename",getIntent().getExtras().getString("linename"));
                    intent.putExtra("deviceid",dataArray.getJSONObject(i).optInt("id"));
                    intent.putExtra("lineid", getIntent().getExtras().getInt("lineid"));

                    startActivity(intent);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    class MyAdapter extends BaseAdapter {
        @Override
        public int getCount() {
            return dataArray.length();
        }

        @Override
        public Object getItem(int position) {
            try {
                return dataArray.get(position);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            convertView = getLayoutInflater().inflate(R.layout.activity_lineiterm, null);

            TextView linename = convertView.findViewById(R.id.linename);
            try {
                linename.setText("设备：" + dataArray.getJSONObject(position).optString("name"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return convertView;
        }


    }


    private void getTasklist(int lineid) {


        ShowLoading();
        RequestParams params = new RequestParams(Config.BaseUrl + "line/devices?lineId=" + lineid);
        params.setUseCookie(true);
        params.addHeader("cookie", CacheDiskUtils.getInstance().getString("cookie"));
        x.http().get(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {

                Log.e("result", result);

                try {
                    dataArray = new JSONArray(result);
                    myadapter.notifyDataSetChanged();
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                Toast.makeText(x.app(), ex.getMessage(), Toast.LENGTH_LONG).show();
            }

            @Override
            public void onCancelled(CancelledException cex) {
                Toast.makeText(x.app(), "cancelled", Toast.LENGTH_LONG).show();
            }

            @Override
            public void onFinished() {
                CancleLoading();
            }
        });
    }
}
